<?php

// @formatter:off
/**
 * @file directories.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: directories.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/directories.php $
 */
// @formatter:on

class ActionControllerContentDirectories extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_DIRECTORY_LIST)) {
				$check = WC_HTTPERROR_FORBIDDEN;

				global $errno;
				global $error;

				$errno = WC_HTTPERROR_FORBIDDEN;
				$error = "You're not allowed to list directories";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcGroup;

		$limiter = "directories";
		$limits = WCFVPager::GetLimitsFor($limiter);

		$this->_manager->PATH = "All Directory (page ".WCFVPager::GetPage().")";

		//
		// Selecting only directories in range.
		$directories = array();
		$dirCount = 1;
		$dirsFull = WCDirectoriesHolder::I()->itemsByGroup($wcGroup);
		uasort($dirsFull, "WCDirectory::NameSort");
		foreach($dirsFull as $directory) {
			if($directory->ok() && ($directory->count > 0 || $directory->icount > 0)) {
				$dirCount++;
			} else {
				continue;
			}
			if($dirCount >= $limits["min"] && $dirCount <= $limits["max"]) {
				$directories[] = $directory;
			}
		}

		WCFVPager::GetAssignsIntoManagerFor($dirCount, $limiter);
		//
		// Creating multi-array by prefix
		$lists = array();
		$ids = 1;
		foreach($directories as $directory) {
			$name = $directory->name;
			$prefix = strtoupper(isset($name[1]) ? $name[1] : "#");
			if($prefix < "A" || $prefix > "Z") {
				$prefix = "#";
			}
			$prefix = "/{$prefix}";
			if(!isset($lists[$prefix])) {
				$lists[$prefix] = array(
					"prefix" => $prefix,
					"list" => array()
				);
			}

			$lists[$prefix]["list"][] = WCFVBoxes::GetDirectoryPreviewAssigns($directory);
		}

		ksort($lists);

		$this->_manager->directories_lists = $lists;

		return $ok;
	}
}
